// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   design_check_ok.java

package draft.design;

import include.nseer_cookie.GetWorkflow;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class design_check_ok extends HttpServlet
{

	public design_check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 design_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		try
		{
			if (design_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String design_ID = request.getParameter("design_ID");
				String product_IDn[] = request.getParameterValues("product_ID");
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				int m = 0;
				for (int j = 1; j < product_IDn.length; j++)
				{
					String sql3 = (new StringBuilder("select * from design_module_details where design_ID='")).append(design_ID).append("' and product_ID='").append(product_IDn[j]).append("'").toString();
					ResultSet rs3 = design_db.executeQuery(sql3);
					if (rs3.next())
						m++;
				}

				if (num == 0 && product_IDn.length == 1)
					response.sendRedirect((new StringBuilder("draft/design/design_ok_a.jsp?design_ID=")).append(design_ID).toString());
				else
				if (m != 0)
				{
					response.sendRedirect((new StringBuilder("draft/design/design_ok_c.jsp?design_ID=")).append(design_ID).toString());
				} else
				{
					int n = 0;
					for (int i = 1; i <= num; i++)
					{
						String tem_amount = (new StringBuilder("amount")).append(i).toString();
						String amount = request.getParameter(tem_amount);
						if (!validata.validata(amount))
							n++;
					}

					String amountn[] = request.getParameterValues("amount");
					for (int j = 1; j < product_IDn.length; j++)
						if (!validata.validata(amountn[j]))
							n++;

					if (n == 0)
					{
						String designer = request.getParameter("designer");
						String bodyc = new String(request.getParameter("module_describe").getBytes("UTF-8"), "UTF-8");
						String module_describe = exchange.toHtml(bodyc);
						String register_time = request.getParameter("register_time");
						String register = request.getParameter("register");
						try
						{
							String sql = (new StringBuilder("update design_module set register_time='")).append(register_time).append("',register='").append(register).append("',designer='").append(designer).append("',module_describe='").append(module_describe).append("' where design_ID='").append(design_ID).append("'").toString();
							design_db.executeUpdate(sql);
							double cost_price_sum = 0.0D;
							for (int i = 1; i <= num; i++)
							{
								String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
								String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
								String tem_type = (new StringBuilder("type")).append(i).toString();
								String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
								String tem_amount = (new StringBuilder("amount")).append(i).toString();
								String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
								String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
								String product_name = request.getParameter(tem_product_name);
								String product_ID = request.getParameter(tem_product_ID);
								String type = request.getParameter(tem_type);
								String product_describe = request.getParameter(tem_product_describe);
								String amount = request.getParameter(tem_amount);
								String amount_unit = request.getParameter(tem_amount_unit);
								String cost_price2 = request.getParameter(tem_cost_price);
								StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
								String cost_price;
								String cost_price1;
								for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
									cost_price1 = tokenTO3.nextToken();

								double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amount);
								cost_price_sum += subtotal;
								String sql1 = (new StringBuilder("update design_module_details set details_number='")).append(i).append("',product_ID='").append(product_ID).append("',product_name='").append(product_name).append("',product_describe='").append(product_describe).append("',type='").append(type).append("',amount='").append(amount).append("',cost_price='").append(cost_price).append("',amount_unit='").append(amount_unit).append("',subtotal='").append(subtotal).append("' where design_ID='").append(design_ID).append("' and product_ID='").append(product_ID).append("'").toString();
								design_db.executeUpdate(sql1);
							}

							String product_namen[] = request.getParameterValues("product_name");
							String typen[] = request.getParameterValues("type");
							String product_describen[] = request.getParameterValues("product_describe");
							String amountm[] = request.getParameterValues("amount");
							String amount_unitn[] = request.getParameterValues("amount_unit");
							String cost_pricen[] = request.getParameterValues("cost_price");
							for (int j = 1; j < product_IDn.length; j++)
							{
								StringTokenizer tokenTO4 = new StringTokenizer(cost_pricen[j], ",");
								String cost_price4;
								String cost_price3;
								for (cost_price4 = ""; tokenTO4.hasMoreTokens(); cost_price4 = (new StringBuilder(String.valueOf(cost_price4))).append(cost_price3).toString())
									cost_price3 = tokenTO4.nextToken();

								double subtotal = Double.parseDouble(cost_price4) * Double.parseDouble(amountm[j]);
								cost_price_sum += subtotal;
								int details_number = num + j;
								String sql4 = (new StringBuilder("insert into design_module_details(design_ID,details_number,product_ID,product_name,type,product_describe,amount,cost_price,amount_unit,subtotal) values ('")).append(design_ID).append("','").append(details_number).append("','").append(product_IDn[j]).append("','").append(product_namen[j]).append("','").append(typen[j]).append("','").append(product_describen[j]).append("','").append(amountm[j]).append("','").append(cost_price4).append("','").append(amount_unitn[j]).append("','").append(subtotal).append("')").toString();
								design_db.executeUpdate(sql4);
							}

							List rsList = GetWorkflow.getList(design_db, "design_config_workflow", "03");
							String elem[] = new String[3];
							String sql2 = "";
							if (rsList.size() == 0)
							{
								sql2 = (new StringBuilder("update design_module set cost_price_sum='")).append(cost_price_sum).append("',check_tag='1',change_tag='1' where design_ID='").append(design_ID).append("'").toString();
								design_db.executeUpdate(sql2);
							} else
							{
								sql2 = (new StringBuilder("delete from design_workflow where object_ID='")).append(design_ID).append("'").toString();
								design_db.executeUpdate(sql2);
								sql2 = (new StringBuilder("update design_module set cost_price_sum='")).append(cost_price_sum).append("',check_tag='0',change_tag='1' where design_ID='").append(design_ID).append("'").toString();
								design_db.executeUpdate(sql2);
								for (Iterator ite = rsList.iterator(); ite.hasNext(); design_db.executeUpdate(sql2))
								{
									elem = (String[])ite.next();
									sql2 = (new StringBuilder("insert into design_workflow(config_id,object_ID,describe1,describe2,type_id) values ('")).append(elem[0]).append("','").append(design_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("','03')").toString();
								}

							}
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect("draft/design/design_ok.jsp?finished_tag=4");
					} else
					{
						response.sendRedirect((new StringBuilder("draft/design/design_ok_b.jsp?design_ID=")).append(design_ID).toString());
					}
				}
				design_db.commit();
				design_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
